StageFits <- function(dat=cm1, main="",case_label="2011 assessment",f=1) {
   subtle.color <- "gray40"
   attach(dat)
   ages <- c(1,3) #age range
   ages.list <- ages[1]:ages[2]
   pred.data = P_ts
   obs.data  = p_ts
   years     = 1978:2010
   nyears <- length(years)
   nages <- length(ages.list)
   mfcol <- c(ceiling(nyears/3),3)
   par(mfcol=mfcol,oma=c(3.5,4.5,3.5,1),mar=c(0,0,0,0))
   cohort.color <- rainbow(mfcol[1]+2)[-c(1:2)]   #use hideous rainbow colors because they loop more gracefully than rich.colors
   # cohort.color <- "yellow" #rainbow(ages[2]+2)[-c(1:2)]   
   ncolors <- length(cohort.color)
   ylim <- c(0,1.10*max(obs.data,pred.data))
   for (yr in 1:nyears) {
      names.arg <- rep("",nages)
      x <- barplot(obs.data[yr,],space=0.2,ylim=ylim,las=1,names.arg=names.arg, cex.names=0.5, 
                   xaxs="i",yaxs="i",border=cohort.color[1:nages],
                  col=cohort.color[1:nages],axes=F,ylab="",xlab="",main=main)

      cohort.color <- c(cohort.color[ncolors],cohort.color[-1*ncolors])  #loop around colors
      if (yr %% mfcol[1] == 0) {
         axis(side=1,at=x,lab=ages.list, line=-0.1,col.axis=subtle.color, 
              col=subtle.color,lwd=0,lwd.ticks=0)  #just use for the labels, to allow more control than names.arg
      }
      if (yr <= mfcol[1]) {
        axis(2,las=1,at=c(0,0.25,0.5),col=subtle.color,col.axis=subtle.color,lwd=0.5)
      }
      par(new=T)
      par(xpd=NA)
      plot(x=x,y=pred.data[yr,],ylim=ylim, xlim=par("usr")[1:2], las=1,xaxs="i",yaxs="i",
          bg="white",fg="brown",
          pch=3,cex=2,axes=F,ylab="",xlab="")
      box(col=subtle.color,lwd=0.5)
      x.pos <- par("usr")[1] + 0.5*diff(par("usr")[1:2])   #par("usr") spits out the current coordinates of the plot window
      y.pos <- par("usr")[3] + 0.85*diff(par("usr")[3:4])   #par("usr") spits out the current coordinates of the plot window
      text(x=x.pos,y=y.pos,years[yr],cex=1.2, col=subtle.color)
      par(xpd=T)
   }
   mtext(side=1,outer=T,"Stage",line=2)
   mtext(side=2,outer=T,"Proportion",line=3.2)
   mtext(side=3,outer=T,line=1.2,"Trawl survey stage composition data")
   mtext(side=3,outer=T,line=0.2,paste("(",case_label,")",sep=""),cex=0.6)
  pred.data = P_ps
  obs.data  = p_ps
  years = yrs_ps
  nyears <- length(years)
  for (yr in 1:nyears) {
   names.arg <- rep("",nages)
   x <- barplot(obs.data[yr,],space=0.2,ylim=ylim,las=1,names.arg=names.arg, cex.names=0.5, 
                xaxs="i",yaxs="i",border=cohort.color[1:nages],
               col=cohort.color[1:nages],axes=F,ylab="",xlab="",main=main)

   cohort.color <- c(cohort.color[ncolors],cohort.color[-1*ncolors])  #loop around colors
   if (yr %% mfcol[1] == 0) {
      axis(side=1,at=x,lab=ages.list, line=-0.1,col.axis=subtle.color, 
           col=subtle.color,lwd=0,lwd.ticks=0)  #just use for the labels, to allow more control than names.arg
   }
   if (yr <= mfcol[1]) {
     axis(2,las=1,at=c(0,0.25,0.5),col=subtle.color,col.axis=subtle.color,lwd=0.5)
   }
   par(new=T)
   par(xpd=NA)
   plot(x=x,y=pred.data[yr,],ylim=ylim, xlim=par("usr")[1:2], las=1,xaxs="i",yaxs="i",
       bg="white",fg="brown",
       pch=3,cex=2,axes=F,ylab="",xlab="")
   box(col=subtle.color,lwd=0.5)
   x.pos <- par("usr")[1] + 0.5*diff(par("usr")[1:2])   #par("usr") spits out the current coordinates of the plot window
   y.pos <- par("usr")[3] + 0.85*diff(par("usr")[3:4])   #par("usr") spits out the current coordinates of the plot window
   text(x=x.pos,y=y.pos,years[yr],cex=1.2, col=subtle.color)
   par(xpd=T)
}
mtext(side=1,outer=T,"Stage",line=2)
mtext(side=2,outer=T,"Proportion",line=3.2)
mtext(side=3,outer=T,line=1.2,"Trawl survey stage composition data")
mtext(side=3,outer=T,line=0.2,paste("(",case_label,")",sep=""),cex=0.6)

  detach(dat)
}
